<?php namespace App\Http\Controllers\user\uc\devicetech;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use Session;
use App\Models\Devices;
use App\Models\Techs;
use App\Models\Domains;
use App\Models\DeviceDomain;
use App\Models\DeviceTech;
use App\Models\DeviceMainBody;
use App\Models\MainBody;
use App\Models\DeviceCases;
use App\Models\DeviceApply;
use App\Models\Projects;
use App\Models\DeviceFeatures;
use App\Models\DeviceFunctions;

use Validator;
use App;
use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;

class DeviceController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$model = Devices::where("uid", $uid);
		if($model->count())
		{
			$numPerPage = 20;
			$count = $model->count();

			$curPage = (int)Input::get("page");
			if(!$curPage){
				$curPage = 1;
			}
			$p = new LengthAwarePaginator($model, $count, $numPerPage, $curPage);
			$p->setPath("");
	        $presenter = new BootstrapPresenter($p);
	        $device = $model->skip(($curPage - 1) * $numPerPage)->take($numPerPage)->get()->toArray();
		}
		else
		{
			$device = [];
			$presenter = [];
		}

		return view('user.uc.devicetech.device', ['data' => $device])->withPresenter($presenter)->withIsDevice(true);
	}

	public function editDevice()
	{
		$did = Input::get("did");
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		$mBody = MainBody::all()->toArray();
		$allProject = Projects::all()->toArray();
		return view('user.uc.devicetech.edit_device', ["techs" => $techs, "domains" => $domains, "mBody" => $mBody, "allProject" => $allProject])->withIsDevice(true);
	}

	public function addDevice()
	{
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		$mBody = MainBody::all()->toArray();
		return view("user.uc.devicetech.add_device", ["techs" => $techs, "domains" => $domains, "mBody" => $mBody])->withIsDevice(true);
	}

	public function deviceDetail()
	{
		return view('user.uc.devicetech.device_detail')->withIsDevice(true);
	}

	public function saveDevice()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$data = Input::all();
			$imgArr = Input::only("image");

			$this->moveImages($imgArr, env("DEVICE_DIR"));

			$data['uid'] = $uid;
			$device = Devices::create($data);
			$deviceTech = [
				"cate_id"	=>	Input::get("tech_id"),
				"device_id"	=>	$device->id
			];
			DeviceTech::create($deviceTech);
			$deviceDomain = ["cate_id" => Input::get("domain_id"), "device_id" => $device->id];
			DeviceDomain::create($deviceDomain);

			$function_name = Input::get("function_name");
			$features_name = Input::get("features_name");
			DeviceFunctions::create(["device_id" => $device->id, "content" => $function_name]);
			DeviceFeatures::create(["device_id" => $device->id, "content" => $features_name]);

			$device_main = Input::get("main_body");
			foreach ($device_main as $main)
			{
				DeviceMainBody::create(["mb_id" => $main, "device_id" => $device->id]);
			}
			$cases = Input::get("cases");
			if($cases){
				foreach ($cases as $key => $value) {
					DeviceCases::create(["name" => $value['name'], "content" => $value['content'], "start_time" => $value['start_time'], "end_time" => $value['end_time'], "device_id" => $device->id]);
				}
			}
			$applys = Input::get("applys");
			if($applys){
				foreach ($applys as $key => $value) {
					DeviceApply::create(["device_id" => $device->id, "project_id" => $value['pid']]);
				}
			}

			return $this->json([]);
		}else{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function updateDevice()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$did = Input::get("did");
			$uid = $this->getUid();
			$model = Devices::where("id", $did)->where("uid", $uid);
			if($model->count()){
				$data = Input::except("main_body", "tech_id", "domain_id", "did", "function_name", "features_name");
				$imgArr = Input::only("image");
				$this->moveImages($imgArr, env("DEVICE_DIR"));
				$model->update($data);
				$tech = DeviceTech::where("device_id", $did);
				$tech->update(["cate_id" => Input::get("tech_id")]);
				$domain = DeviceDomain::where("device_id", $did);
				$domain->update(["cate_id" => Input::get("domain_id")]);
				$main_model = DeviceMainBody::where("device_id", $did);
				$main_model->delete();
				$device_main = Input::get("main_body");

				$function_name = Input::get("function_name");
				$features_name = Input::get("features_name");
				$function_model = DeviceFunctions::where("device_id", $did);
				$function_model->update(["content" => $function_name]);
				$features_model = DeviceFeatures::where("device_id", $did);
				$features_model->update(["content" => $features_name]);


				foreach ($device_main as $main)
				{
					DeviceMainBody::create(["mb_id" => $main, "device_id" => $did]);
				}
				return $this->json([]);
			}else{
				return;
			}

		}else{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function getDevice()
	{
		$did = (int)Input::get("did");
		$model = Devices::with(['deviceDomain', 'deviceTech', 'deviceDomain.domain', "mainbody", "deviceFunctions", "deviceFeatures"])->where("id", $did);
		if($model->count()){
			$data = $model->get()[0]->toArray();
			return $this->json($data);
		}else{
			return App::abort(404);
		}
	}


	public function getDeviceCases()
	{
		$did = Input::get("did");
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getCaseDetail()
	{
		$cid = Input::get("cid");
		$data = DeviceCases::where("id", $cid)->get()[0]->toArray();
		return $this->json(["data" => $data]);
	}

	public function updateCase()
	{
		$cid = Input::get("cid");
		$did = Input::get("device_id");
		$model = DeviceCases::where("id", $cid);
		$data = Input::except("cid", "device_id");
		$model->update($data);
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function addCase()
	{
		$device_id = Input::get("device_id");
		$data = Input::except("cid");
		DeviceCases::create($data);
		$data = DeviceCases::where("device_id", $device_id)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function deleteCases()
	{
		$cid = Input::get("cid");
		$did = Input::get("did");
		$model = DeviceCases::where("id", $cid);
		$model->delete();
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function saveApply()
	{
		$pid = Input::get("pid");
		$did = Input::get("did");
		DeviceApply::create(["project_id" => $pid, "device_id" => $did]);
		$data = DeviceApply::with(['project'])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function deleteApply()
	{
		$did = Input::get("did");
		$aid = Input::get("aid");
		$model = DeviceApply::where("id", $aid);
		$model->delete();
		$data = DeviceApply::with(["project"])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getDeviceApply()
	{
		$did = Input::get("did");
		$data = DeviceApply::with(["project"])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function deviceShow()
	{
		$did = Input::get("did");
		$model = Devices::where("id", $did);
		if($model->count()){
			$verify_state = $model->pluck("verify_state");
			if($verify_state == 0){
				return self::error(self::IS_AUDITING);
			}else if($verify_state == 2){
				return self::error(self::AUDIT_NOT_PASS);
			}else{
				return $this->json([]);
			}
		}
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
